package com.example.zhinongbao.presenter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import androidx.annotation.NonNull;

import com.example.zhinongbao.base.WeakRefHandler;
import com.example.zhinongbao.contract.GoodsSearchContract;
import com.example.zhinongbao.entity.Goods;

import java.util.ArrayList;
import java.util.List;


public class GoodsSearchPresenter implements GoodsSearchContract.IGoodsSearchPresenter {

    private static final int WHAT_GET_GOODS_LIST = 0;
    private GoodsSearchContract.IGoodsSearchContractView mGoodsSearchContractView;
    private Context mContext;

    public GoodsSearchPresenter(GoodsSearchContract.IGoodsSearchContractView goodsSearchContractView, Context context) {
        mGoodsSearchContractView = goodsSearchContractView;
        mContext = context;
    }

    Handler mHandler = new WeakRefHandler<GoodsSearchPresenter>(Looper.getMainLooper(), this, new WeakRefHandler.Callback() {
        @Override
        public void handleMessage(Object associatedObject, Message msg) {
            int what = msg.what;
            switch (what) {
                case WHAT_GET_GOODS_LIST:
                    mGoodsSearchContractView.showGoodsList((List<Goods>) msg.obj);
                    break;
            }
        }
    });

    @Override
    public void getGoodsList(String key) {
        List<Goods> list = new ArrayList<>();
        new Thread(new Runnable() {
            @SuppressLint("Range")
            @Override
            public void run() {
                //此处异步进行数据查询
                Uri uri = Uri.parse("content://com.example.zhinongbao.provider/goods");
                Cursor cursor = mContext.getContentResolver().query(uri, null, "goods_name like ?", new String[]{"%" + key + "%"}, null);
                try {
                    if (cursor != null && cursor.moveToFirst()) {
                        do {
                            Goods goods = new Goods();
                            goods.setId(cursor.getInt(cursor.getColumnIndex("_id")));
                            goods.setName(cursor.getString(cursor.getColumnIndex("goods_name")));
                            goods.setPrice(cursor.getDouble(cursor.getColumnIndex("goods_price")));
                            goods.setUnit(cursor.getString(cursor.getColumnIndex("goods_unit")));
                            goods.setBrowseCount(cursor.getInt(cursor.getColumnIndex("goods_browse")));
                            goods.setMessage(cursor.getString(cursor.getColumnIndex("goods_message")));
                            Log.d("", "cursor = " + cursor.getCount());
                            list.add(goods);
                        } while (cursor.moveToNext());
                    }
                } finally {
                    if (cursor != null) {
                        cursor.close();
                    }
                }
                Uri photoUri = Uri.parse("content://com.jlastu.zhinongbao.provider/photo");
                Cursor photoCursor = mContext.getContentResolver().query(
                        photoUri, null, null, null);
                if (photoCursor != null && photoCursor.moveToFirst()) {
                    do {
                        Log.d("", "photo_id = " + photoCursor.getInt(cursor.getColumnIndex("_id")));
                        Log.d("", "photo_miage = " + photoCursor.getBlob(2));
                    } while (photoCursor.moveToNext());
                }
                //子线程通过Handler发送消息，完成线程之间的通信。
                Log.d("", "list = " + list.size());
                Message message = Message.obtain();
                message.obj = list;
                message.what = WHAT_GET_GOODS_LIST;
                mHandler.sendMessage(message);
            }
        }).start();
    }
}
